/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sjtu.mail;

import java.security.*;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author p w
 */
public class MailTest {

//    public static void main(String[] args) {
//        //这个类主要是设置邮件   
//        MailSenderInfo mailInfo = new MailSenderInfo();
//        mailInfo.setMailServerHost("smtp.gmail.com");
////        mailInfo.setMailServerPort("465");
//        mailInfo.setValidate(true);
//        mailInfo.setUserName("pengweiaolu@gmail.com");
//        mailInfo.setPassword("19871212");//您的邮箱密码    
//        mailInfo.setFromAddress("pengweiaolu@gmail.com");
//        mailInfo.setToAddress("pengweiaolu@gmail.com");
//        mailInfo.setSubject("设置邮箱标题 如http://www.guihua.org 中国桂花网");
//        mailInfo.setContent("设置邮箱内容 如http://www.guihua.org 中国桂花网 是中国最大桂花网站==");
//        //这个类主要来发送邮件   
//        SimpleMailSender sms = new SimpleMailSender();
//        sms.sendTextMail(mailInfo);//发送文体格式    
// //       sms.sendHtmlMail(mailInfo);//发送html格式   
//    }
    public static void main(String args[]) {
        MailTest mail = new MailTest();
        Session session = mail.initMes();
        mail.sendMes("pengwei-aolu@sjtu.edu.cn", session, "how are you from new book", "new subject");
//        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
//        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
//        Properties props = System.getProperties();
//        props.setProperty("mail.smtp.host", "smtp.gmail.com");
//        props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
//        props.setProperty("mail.smtp.socketFactory.fallback", "false");
//        props.setProperty("mail.smtp.port", "465");
//        props.setProperty("mail.smtp.socketFactory.port", "465");
//        props.put("mail.smtp.auth", "true");
//
//        Session session = Session.getDefaultInstance(props, new Authenticator() {
//
//            @Override
//            protected PasswordAuthentication getPasswordAuthentication() {
//                return new PasswordAuthentication("pengweiaolu", "19871212");
//            }
//        });
//        Message msg = new MimeMessage(session);
//        try {
//            // -- Set the FROM and TO fields --
//            msg.setFrom(new InternetAddress("pengweiaolu" + "@gmail.com"));
//            msg.setRecipients(Message.RecipientType.TO,
//                    InternetAddress.parse("pengwei-aolu@sjtu.edu.cn", false));
//            msg.setSubject("Hello");
//            msg.setText("How are you");
//            msg.setSentDate(new Date());
//            Transport.send(msg);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

    public Session initMes() {
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
        Properties props = System.getProperties();
        props.setProperty("mail.smtp.host", "smtp.gmail.com");
        props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.port", "465");
        props.setProperty("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props, new Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("pengweiaolu", "19871212");
            }
        });
        return session;
    }

    public void sendMes(String MailAddress, Session session, String text, String subject) {
        Message msg = new MimeMessage(session);
        try {
            // -- Set the FROM and TO fields --
            msg.setFrom(new InternetAddress("pengweiaolu" + "@gmail.com"));
            msg.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(MailAddress, false));
            msg.setSubject(subject);
            msg.setText(text);
            msg.setSentDate(new Date());
            Transport.send(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
